package com.chuanjiabao.wisdom.util;

import android.annotation.SuppressLint;
import android.util.Log;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.application.FApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {


    public static String timeToStr(String time) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = simpleDateFormat.parse(time + " 00:00:00");
            return date.getTime() / 1000 + "";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间转化为聊天界面显示字符串
     *
     * @param timeStamp 单位为秒
     */
    public static String ChatToTimeStr(long timeStamp) {
        if (timeStamp == 0) return "";
        Calendar inputTime = Calendar.getInstance();
        inputTime.setTimeInMillis(timeStamp * 1000);
        Date currenTimeZone = inputTime.getTime();
        Calendar calendar = Calendar.getInstance();
        if (!calendar.after(inputTime)) {
            //当前时间在输入时间之前
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + FApplication.getInstance().getResources().getString(R.string.time_year) + "MM" + FApplication.getInstance().getResources().getString(R.string.time_month) + "dd" + FApplication.getInstance().getResources().getString(R.string.time_day));
            return sdf.format(currenTimeZone);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (calendar.before(inputTime)) {
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            return sdf.format(currenTimeZone);
        }
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        if (calendar.before(inputTime)) {
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat sdf = new SimpleDateFormat("M" + FApplication.getInstance().getResources().getString(R.string.time_month) + "d" + FApplication.getInstance().getResources().getString(R.string.time_day) + " HH:mm");
            return sdf.format(currenTimeZone).split(" ")[0];
        } else {
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + FApplication.getInstance().getResources().getString(R.string.time_year) + "MM" + FApplication.getInstance().getResources().getString(R.string.time_month) + "dd" + FApplication.getInstance().getResources().getString(R.string.time_day) + " HH:mm");
            return sdf.format(currenTimeZone).split(" ")[0];
        }
    }

    /**
     * 将毫秒转化成固定格式的时间
     * 时间格式: yyyy-MM-dd HH:mm:ss
     */
    public static String getDateTimeFromMillisecond(Long millisecond) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = new Date(millisecond);
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }


    public static String getTime() {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    /**
     * 将总秒数转换为时分秒表达形式
     *
     * @param seconds 任意秒数
     * @return %s:%s:%s
     */
    public static String formatTime(long seconds) {
        long hh = seconds / 60 / 60;
        long mm = (seconds - hh * 60 * 60) > 0 ? (seconds - hh * 60 * 60) / 60 : 0;
        long ss = seconds < 60 ? seconds : seconds % 60;
        return (hh == 0 ? "" : hh + ":")
                + (mm == 0 ? "00:" : (mm < 10 ? "0" + mm : mm) + ":")
                + (ss == 0 ? "" : (ss < 10 ? "0" + ss : ss));
    }

}
